package cn.lzx.service.impl;

import cn.lzx.domain.Product;
import cn.lzx.dto.ProductDto;
import cn.lzx.feign.StoreService;
import cn.lzx.mapper.ProductMapper;
import cn.lzx.pojo.StorePojo;
import cn.lzx.service.ProductService;
import cn.lzx.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreService storeService;

    @Override
    public JsonResult add(ProductDto productDto) {

        try {
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            StorePojo storePojo = new StorePojo();
            storePojo.setNumber(productDto.getNumber());
            storePojo.setProductId(product.getId());
            JsonResult add = storeService.add(storePojo);
            if (!add.getSuccess()){
                return add;
            }

            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"添加失败");
        }
    }
}
